import 'package:easy_refresh/easy_refresh.dart';
import 'package:enter_prise_web/common/constants.dart';
import 'package:enter_prise_web/page/main/main_controller.dart';
import 'package:enter_prise_web/widgets/NotFound.dart';
import 'package:enter_prise_web/widgets/timeline.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MainPage extends StatelessWidget {
  MainPage({super.key});
  MainController mainController = Get.find();

  final EasyRefreshController refreshController = EasyRefreshController(
      controlFinishRefresh: true, controlFinishLoad: true);
  bool isShow = true;
  // 创建签到人员头像组件

  _createStaffWidgets({String? uid, String? name}) {
    return Container(
      child: Column(
        children: [
          Container(
            width: 70,
            height: 70,
            padding:
                const EdgeInsets.only(left: 10, bottom: 10, top: 10, right: 10),
            child: CircleAvatar(
              backgroundImage: NetworkImage(
                  'http://flycan.ren:8026/avatar/getByUid?uid=$uid'),
            ),
          ),
          Text(name ?? ""), // 显示人员姓名
        ],
      ),
    );
  }

  List<Widget> _createAvatarWidgets(MainController c) {
    List<Widget> widgets = [];
    c.users.asMap().forEach((key, value) {
      widgets.add(_createStaffWidgets(name: c.users[key]['uid'], uid: c.users[key]['uid']));
    });

    return widgets;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: kDefaualtBgColor,
      body: Stack(
        children: [
          Positioned(
            top: 10,
            left: 30,
            right: 30,
            child: GetBuilder<MainController>(
                id: "userlist",
                builder: (_) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(bottom: 8.0),
                        child: Row(
                          children: [
                            const Text(
                              "今日到勤人员",
                              style: TextStyle(fontSize: 20),
                            ),
                            Text(
                              "(到勤人数为:${_.users.length}人)",
                              style: const TextStyle(
                                  fontSize: 16, color: kTextColor),
                            ),
                            Expanded(
                              child: Container(),
                            ),
                            Container(
                              margin: const EdgeInsets.only(right: 10),
                              child: IconButton(
                                icon: const Icon(
                                  Icons.refresh,
                                  size: 30,
                                ),
                                onPressed: () {
                                  //刷新
                                  _.refreshUserList();
                                  afterRefresh();
                                },
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            color: Colors.white),
                        width: double.infinity,
                        height: 280,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Wrap(
                                alignment: WrapAlignment.start,
                                children: _createAvatarWidgets(_))
                          ],
                        ),
                      ),
                    ],
                  );
                }),
          ),
          Positioned(
              bottom: 40,
              left: 30,
              right: 30,
              top: 380,
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20),
                ),
                height: MediaQuery.of(context).size.height - 310 - 40,
                child: Padding(
                    padding: const EdgeInsets.only(left: 20.0, top: 8.0),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const Padding(
                            padding: EdgeInsets.only(bottom: 12.0, top: 4),
                            child: Text(
                              "提交记录",
                              style: TextStyle(fontSize: 20),
                            ),
                          ),
                          Expanded(
                            child: GetBuilder<MainController>(
                                id: "workRecordToday",
                                builder: (controller) {
                                  var workRecord = controller.workRecordOfToday;
                                  return EasyRefresh(
                                      controller: refreshController,
                                      onRefresh: () async {
                                        controller.refreshData();
                                        refreshController.finishRefresh();
                                      },
                                      child: controller
                                              .workRecordOfToday.isEmpty
                                          ? const Center(
                                              child: NotFound(
                                                child: Text("您好像来到了一片不毛之地"),
                                              ),
                                            )
                                          : ListView.builder(
                                              shrinkWrap: true,
                                              itemBuilder: (context, index) {
                                                var work = workRecord[index];
                                                String owner = work['owner'];
                                                String objectName =
                                                    work['objectName'];
                                                int number = work['number'];
                                                String time = work['time'];

                                                return timeline(
                                                  bgColor: Colors.orange,
                                                  isLast: index ==
                                                      workRecord.length - 1,
                                                  title:
                                                      "$owner提交了$number个$objectName",
                                                  subtitle: "提交时间：$time",
                                                  content:
                                                      "备注:${work['commentary']}",
                                                );
                                              },
                                              itemCount: controller
                                                  .workRecordOfToday.length,
                                            ));
                                }),
                          ),
                        ])),
              )),
        ],
      ),
    );
  }
}
